// Created by carolsail

import { ENUM_GAME_STATUS } from '../Enum';

const STORAGE_KEY = 'CC2_WATER_SORT_PUZZLE'

export const DATA_GAME = {
    nums: 4, // 瓶子层数
    colors: ['#FF789F', '#F7AD54', '#69BCF9', '#AAD736', '#305147', '#BC68EF', '#E6572F', '#F3F688', '#1229F4', '#06AD4C', '#5306AD', '#525054'] // 颜色
}

/**
 * bottles: 瓶子总数量
 * empty: 空瓶子数量
 * locked: 锁数量
 */
export const DATA_LEVEL = [
    { bottles: 2, empty: 0, locked: 0, data: [[3, 3, 3], [3]] },
    { bottles: 3, empty: 1, locked: 0, data: [[1, 1, 2, 2], [2, 2, 1, 1]] },
    { bottles: 4, empty: 2, locked: 1, data: [[0, 3, 0, 3], [3, 0, 3, 0]] },
    { bottles: 5, empty: 1, locked: 1, data: [[1], [2], [2, 1, 2], [1, 2, 1]] },
    { bottles: 6, empty: 3, locked: 2, data: [[0, 1, 2, 0], [2, 0, 1, 1], [2, 0, 1, 2]] },
    { bottles: 7, empty: 3, locked: 1, data: [] },
    { bottles: 8, empty: 3, locked: 1, data: [[0, 0, 1, 2], [3, 4, 1, 2], [4, 1, 3, 1], [3, 0, 2, 4], [0, 3, 2, 4]] },
    { bottles: 8, empty: 2, locked: 0, data: [] },
    { bottles: 8, empty: 3, locked: 1, data: [[0, 6, 1, 6], [3, 3, 1, 6], [2, 2, 3, 6], [0, 2, 1, 3], [0, 1, 0, 2]] },
    { bottles: 9, empty: 3, locked: 1, data: [] },
    { bottles: 9, empty: 2, locked: 0, data: [] },
    { bottles: 10, empty: 3, locked: 1, data: [[0, 0, 1, 2], [3, 4, 1, 4], [0, 0, 3, 4], [2, 5, 3, 4], [6, 3, 5, 1], [6, 5, 6, 2], [5, 2, 1, 6]] },
    { bottles: 10, empty: 3, locked: 1, data: [] },
    { bottles: 10, empty: 2, locked: 0, data: [] },
    { bottles: 11, empty: 3, locked: 1, data: [] },
    { bottles: 11, empty: 2, locked: 0, data: [] },
    { bottles: 12, empty: 3, locked: 1, data: [] },
    { bottles: 12, empty: 2, locked: 0, data: [] },
    { bottles: 13, empty: 3, locked: 1, data: [] },
    { bottles: 13, empty: 2, locked: 0, data: [] },
    { bottles: 14, empty: 3, locked: 1, data: [] },
    { bottles: 14, empty: 3, locked: 1, data: [] },
    { bottles: 14, empty: 3, locked: 1, data: [] },
    { bottles: 14, empty: 3, locked: 1, data: [] },
    { bottles: 14, empty: 3, locked: 1, data: [] },
    { bottles: 14, empty: 2, locked: 0, data: [] },
    { bottles: 14, empty: 2, locked: 0, data: [] },
    { bottles: 14, empty: 2, locked: 0, data: [] },
    { bottles: 14, empty: 2, locked: 0, data: [] },
    { bottles: 14, empty: 2, locked: 0, data: [] },
]
export const DATA_LEVEL_DIFFICULTY = [
    { bottles: 54, empty: 3, locked: 1, data: [] },
]

export default class DataManager {

    private static _instance: any = null

    static getInstance<T>(): T {
        if (this._instance === null) {
            this._instance = new this()
        }

        return this._instance
    }

    static get instance() {
        return this.getInstance<DataManager>()
    }

    // 游戏状态
    status: ENUM_GAME_STATUS = ENUM_GAME_STATUS.UNRUNING
    // 加载进度
    loadingRate: number = 0
    // 声音开启
    _isMusicOn: boolean = true
    _isSoundOn: boolean = true
    // 更多游戏
    games: any[] = [
        { title: '进厂打螺丝', icon: 'logo_screw', appid: 'wx830aab3e2bbe7651', url: 'https://store.cocos.com/app/search?name=carolsail' },
        { title: '趣味聊天', icon: 'logo_chat', appid: 'wx553839f7f814c2ee', url: 'https://store.cocos.com/app/search?name=carolsail' },
        { title: '挪车大挑战', icon: 'logo_car', appid: 'wx0d61e0041ca571cc', url: 'https://store.cocos.com/app/search?name=carolsail' },
        { title: '小鸟消消乐', icon: 'logo_bird', appid: 'wxefd5a4ddd8e31b44', url: 'https://store.cocos.com/app/search?name=carolsail' },
        { title: '胶囊2048', icon: 'logo_2048', appid: 'wx81ee25b02b8438df', url: 'https://store.cocos.com/app/search?name=carolsail' },
    ]
    // 关卡
    level: number = 1
    levelMax: number = 1
    // 皮肤
    skinTotal: number = 10
    skinIndex: number = 1
    skinUnlocked: number[] = [1]
    // 体力
    power: number = 10 // 初始体力
    powerMax: number = 10 // 体力最大值
    powerCollectByVideo: number = 1 // 广告回复
    powerRefreshTime: number = 60 // 间隔刷新
    lastPowerRefreshTime: number = 0 // 每次刷新纪录点
    lastPowerUpdateTime: number = 0 //  上一次更新时间
    tool: number = 0
    // 重置技能
    skillReset: boolean = false
    levelData: { data: number[], isLocked: boolean }[] = []
    // 关卡模式
    stageModel: boolean = false //fasle 普通模式
    //挑战模式 开始时间
    challegeTimeStart = 0
    get isMusicOn() {
        return this._isMusicOn
    }

    set isMusicOn(data: boolean) {
        this._isMusicOn = data
    }

    get isSoundOn() {
        return this._isSoundOn
    }

    set isSoundOn(data: boolean) {
        this._isSoundOn = data
    }

    hasSkinIndex(index: number) {
        return this.skinUnlocked.indexOf(index) != -1
    }

    addSkinIndex(index: number) {
        if (this.skinUnlocked.indexOf(index) == -1) {
            this.skinUnlocked.push(index)
            this.save()
        }
    }

    reset() {
        this.status = ENUM_GAME_STATUS.UNRUNING
    }

    save() {
        cc.sys.localStorage.setItem(STORAGE_KEY, JSON.stringify({
            isSoundOn: this.isSoundOn,
            isMusicOn: this.isMusicOn,
            level: this.level,
            levelMax: this.levelMax,
            skinIndex: this.skinIndex,
            skinUnlocked: this.skinUnlocked,
            power: this.power,
            lastPowerRefreshTime: this.lastPowerRefreshTime,
            lastPowerUpdateTime: this.lastPowerUpdateTime,
            tool: this.tool
        }))
    }

    restore() {
        const _data = cc.sys.localStorage.getItem(STORAGE_KEY) as any
        try {
            const data = JSON.parse(_data)
            this.isMusicOn = data?.isMusicOn === false ? false : true
            this.isSoundOn = data?.isSoundOn === false ? false : true
            this.level = typeof data.level == 'number' ? data.level : 1
            this.levelMax = typeof data.levelMax == 'number' ? data.levelMax : 1
            this.skinIndex = typeof data.skinIndex == 'number' ? data.skinIndex : 1
            this.skinUnlocked = data?.skinUnlocked || [1]
            this.power = typeof data.power == 'number' ? data.power : 10
            this.lastPowerRefreshTime = typeof data.lastPowerRefreshTime == 'number' ? data.lastPowerRefreshTime : 0
            this.lastPowerUpdateTime = typeof data.lastPowerUpdateTime == 'number' ? data.lastPowerUpdateTime : 0
            this.tool = typeof data.tool == 'number' ? data.tool : 0
        } catch {
            this.isMusicOn = true
            this.isSoundOn = true
            this.level = 1
            this.levelMax = 1
            this.skinIndex = 1
            this.skinUnlocked = [1]
            this.power = 10
            this.lastPowerRefreshTime = 0
            this.lastPowerUpdateTime = 0
            this.tool = 0
        }
    }
}
